package com.product.bargain.service.impl;

import com.product.bargain.dao.BargainActivityDao;
import com.product.bargain.dao.BargainRecordDao;
import com.product.bargain.entity.BargainActivityEntity;
import com.product.bargain.entity.BargainRecordEntity;
import com.product.bargain.util.JwtUtil;
import com.product.bargain.vo.BargainShopVo;
import com.product.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.product.common.utils.PageUtils;
import com.product.common.utils.Query;

import com.product.bargain.dao.MybargainDao;
import com.product.bargain.entity.MybargainEntity;
import com.product.bargain.service.MybargainService;


@Service("mybargainService")
public class MybargainServiceImpl extends ServiceImpl<MybargainDao, MybargainEntity> implements MybargainService {
    @Autowired
    private MybargainDao mybargainDao;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private BargainActivityDao bargainActivityDao;
    @Autowired
    private BargainRecordDao bargainRecordDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MybargainEntity> page = this.page(
                new Query<MybargainEntity>().getPage(params),
                new QueryWrapper<MybargainEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void addMyBargain(BargainShopVo bargainShopVo) {
        //添加到我的砍价中mybargainEntity
        MybargainEntity mybargainEntity1 = new MybargainEntity();
        mybargainEntity1.setBargainId(bargainShopVo.getBargainId());
        mybargainEntity1.setGoodsId(bargainShopVo.getGoodsId());
        mybargainEntity1.setGoodsName(bargainShopVo.getGoodsName());
        mybargainEntity1.setGoodsPrice(bargainShopVo.getGoodsPrice());
        mybargainEntity1.setCreatetime(new Date());
        mybargainEntity1.setEndtime(bargainShopVo.getEndtime());
        mybargainEntity1.setUserId(bargainShopVo.getUid());
        mybargainEntity1.setUserName(bargainShopVo.getUname());
        mybargainDao.insert(mybargainEntity1);
        System.out.println("生成我的抢购");
    }

    @Override
    public R shareBargain(String uname,Integer id) {
        //使用jwt生成链接
        String path = jwtUtil.createJWT(uname, uname, "path");
        //生成后的链接存入redis中.以防我们后期的一个判断
        redisTemplate.opsForValue().set(uname,path);
        //返回分享链接到前台
        return R.ok().put("path","http://localhost:88/api/bargain/mybargain/help/"+path+"/"+uname+"/"+id);
    }

    @Override
    public R friendBargain(String id, String uname, Integer userId) {
        //查询
        MybargainEntity mybargainEntity = mybargainDao.selectById(id);
        //首先判断该用户是否该用户砍价过
        String s = (String) redisTemplate.boundHashOps(id).get(uname);
        if (s!=null && s.equals(uname)){//砍价过了
            return R.error("已砍价过了").put("code","200");
        }else if (s==null){//砍价
            Long bargainId = mybargainEntity.getBargainId();
            //查询砍多少钱
            BargainActivityEntity bargainActivityEntity = bargainActivityDao.selectById(bargainId);
            if (mybargainEntity.getGoodsPrice()<=bargainActivityEntity.getLowprice()){
                return R.error("已到最低价").put("code","300");
            }else{
                //砍价
                mybargainEntity.setGoodsPrice(mybargainEntity.getGoodsPrice()-bargainActivityEntity.getKnifemoney());
                mybargainDao.updateById(mybargainEntity);
                //添加到记录表
                BargainRecordEntity bargainRecordEntity = new BargainRecordEntity();
                bargainRecordEntity.setBargainId(mybargainEntity.getBargainId());
                bargainRecordEntity.setUserId(userId);
                bargainRecordEntity.setMybargainId(mybargainEntity.getId());
                bargainRecordEntity.setKnifemoney(bargainActivityEntity.getKnifemoney());
                bargainRecordEntity.setHelpdate(new Date());
                bargainRecordDao.insert(bargainRecordEntity);
                redisTemplate.boundHashOps(id).put(uname,uname);
                return R.ok("砍价成功!").put("code","400");
            }
        }
        return null;
    }

    @Override
    public MybargainEntity findByUser(String id) {
        return mybargainDao.selectById(id);
    }


}